package uk.ac.strath.cis.metric_spaces.distance;

import java.util.Iterator;

import uk.ac.strath.cis.collections.Point;
import uk.ac.strath.cis.metric_spaces.framework.Distance;

public abstract class PointDistance implements Distance<Point> {

    /* (non-Javadoc)
     * @see uk.ac.strath.cis.rmoss.metric_spaces.framework.Distance#distance(java.lang.Object, java.lang.Object)
     */
    public double distance(Point p1, Point p2) {
        if (!p1.isSameDimension(p2)) {
            throw new IllegalArgumentException("Two points must be of same dimension");
        }
        Iterator<Double> it1 = p1.iterator();
        Iterator<Double> it2 = p2.iterator();

        double acc = accumulator();

        while (it1.hasNext() && it2.hasNext()) {
            Double n1 = it1.next();
            Double n2 = it2.next();

            acc = algorithm(acc, n1, n2);
        }
        return acc;
    }

    abstract protected double algorithm(double acc, double n1, double n2);
    
    /**
     * override to set initial accumulator value
     * @return
     */
    protected double accumulator(){
    	return 0.0;
    }
}
